package com.spring.framework.transaction.example;

import com.spring.framework.transaction.model.TxStudy;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xuweizhi
 * @since 2021/08/03 13:55
 */
@Service
@SuppressWarnings("all")
public class TxStudyServiceImpl implements TxStudyService {

	/**
	 * jdbc模版，用于操作数据库
	 */
	@Resource
	private JdbcTemplate jdbcTemplate;

	/*这里仅仅是为了测试方便，省去了DAO/Mapper层*/

	@Override
	public TxStudy getTxStudy(Long id) {
		String sql = "select * from tx_study where id = ?";
		return jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(TxStudy.class), id);
	}

	@Override
	public List<TxStudy> getTxStudy(String name) {
		String sql = "select * from tx_study where name like ?";
		return jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(TxStudy.class), "%" + name + "%");
	}

	@Override
	public void insertTxStudy(TxStudy txStudy) {
		String sql = "insert into tx_study (name,age) values (?,?)";
		jdbcTemplate.update(sql, txStudy.getName(), txStudy.getAge());
	}

	@Override
	public void updateTxStudy(TxStudy txStudy) {
		String sql = "UPDATE tx_study SET name=?, age=? where id = ?";
		jdbcTemplate.update(sql, txStudy.getName(), txStudy.getAge(), txStudy.getId());
	}
}
